package com.raymond.sgip.codec.decoding;

import com.raymond.client.msg.BaseAgreement;
import com.raymond.sgip.constants.SgipConstants;
import com.raymond.sgip.msg.request.ReportRequestMessage;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;

import java.util.List;

/**
 * 状态报告和上行解码
 *
 * @author :  raymond
 * @version :  V1.0
 * @date :  2021-08-06 10:48
 */
public class ReportRequestMessageDecoder extends BaseSgipDecoder {
    @Override
    public void decode0(ChannelHandlerContext ctx, BaseAgreement msg, List<Object> out) {
        ReportRequestMessage requestMessage = new ReportRequestMessage(msg.getSequenceId());
        ByteBuf bodyBuffer = Unpooled.wrappedBuffer(msg.getBodyBuffer());
        long nodeId = bodyBuffer.readUnsignedInt();
        int timestamp = bodyBuffer.readInt();
        int sequenceId = bodyBuffer.readInt();
        requestMessage.setSubmitSequenceNumber(nodeId + "" + timestamp + sequenceId);
        requestMessage.setReportType(bodyBuffer.readUnsignedByte());
        requestMessage.setUserNumber(bodyBuffer.readCharSequence(21, SgipConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());
        requestMessage.setState(bodyBuffer.readUnsignedByte());
        requestMessage.setErrorCode(bodyBuffer.readUnsignedByte());
        requestMessage.setReserve(bodyBuffer.readCharSequence(8, SgipConstants.DEFAULT_TRANSPORT_CHARSET).toString().trim());
        out.add(requestMessage);
    }
}
